using System.Diagnostics;
using _ReplaceString_.UI;
using _ReplaceString_.UI.Styles;

namespace _ReplaceString_.UI.Elements;

public class TopGroup : UIBase
{
	private static Dictionary<UIElement, TopGroup> _containers = new();

	public static TopGroup Create(UIElement ui)
	{
		var parent = ui;
		while (parent is not UIState)
		{
			Debug.Assert(parent != null);
			parent = parent.Parent;
		}
		if (!_containers.TryGetValue(parent, out var container))
		{
			_containers[parent] = container = new TopGroup();
			container.AddStyle(UISize.Fill);
			parent.Elements.Insert(0, container);
			container.Parent = parent;
			container.Recalculate();
			ReplaceString.Instance.OnUnload += container.Remove;
		}
		container.Activate();

		return container;
	}

	public override void OnDeactivate()
	{
		RemoveAllChildren();
	}
}